package com.jinrustar.httpfileserver.core.util;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by liduanwei1988@163.com on 2016/12/2 0002.
 */
public class CommonStringUtils {
    public static boolean isEmpty(String text) {
        return null == text || "".equals(text);
    }


    public static boolean isBlank(String str) {
        int strLen;
        if (str != null && (strLen = str.length()) != 0) {
            for (int i = 0; i < strLen; ++i) {
                if (!Character.isWhitespace(str.charAt(i))) {
                    return false;
                }
            }

            return true;
        } else {
            return true;
        }
    }


    // 特殊字符处理
    public static String urlEncode(String src) throws UnsupportedEncodingException {
        return URLEncoder.encode(src, "UTF-8").replace("+", "%20");
    }


    /*字符串转unicode编码*/
    public static String toUnicode(String s) {
        StringBuilder unicodeStr = new StringBuilder();
        for (int i = 0; i < s.length(); ++i) {
            unicodeStr.append("\\u" + Integer.toHexString(s.charAt(i)));
        }
        return unicodeStr.toString();
    }


    /**
     * 中文转Unicode
     * @param cn
     * @return
     */
    public static String cnToUnicode(String cn) {
        char[] chars = cn.toCharArray();
        String returnStr = "";

        Pattern pattern = Pattern.compile("[\\u4e00-\\u9fa5]"); /*匹配中文*/

        for (int i = 0; i < chars.length; i++) {
            Matcher matcher = pattern.matcher(chars[i]+"");
            if(matcher.find()){ /*是中文字符*/
                returnStr += "\\u" + Integer.toString(chars[i], 16);
            }else{
                returnStr += chars[i];
            }

        }
        return returnStr;
    }


    public static void main(String... args) throws UnsupportedEncodingException {
        String s = "账号已存在或其它错误";
        s = "公司名不能为空";
        String unicodeStr = toUnicode(s);
        System.out.println(unicodeStr);

    }
}